package com.shiplanet.action;

import java.util.Date;
import java.util.List;


import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.shiplanet.entity.User;
import com.shiplanet.entity.Visitor;
import com.shiplanet.service.UserService;

@Controller
public class UserAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	@Resource
	private UserService userService;

	// parameters for save()
	private Visitor visitor;

	public String save() {
		visitor.setVisitTime(new Date());

		if (userService.saveVisitor(visitor) != null) {
			return "success";
		} else {
			return ERROR;
		}
	}

	// parameters for mail
	private User user;

	private String exclusionEmailList;
	
	private String next;
	
	private String emailSubject;

	private String emailTemplate;

	private String sendFrom;

	private String sendTo;



	public String mail() {

		if (user == null)
			return "sendemailpage1";

		if (next != null) {
			if (user.getRegion().equals("0")) {
				user.setRegion(null);
			}

			if (user.getCountry().equals("0")) {
				user.setCountry(null);
			}

			if (user.getCompanyType().equals("0")) {
				user.setCompanyType(null);
			}

			if (user.getCompanySubType().equals("0")) {
				user.setCompanySubType(null);
			}

			List<User> userList = userService.findUsersByExample(user);
			
			sendTo = userService.generateSendToList(userList);
			
			return "sendemailpage2";
		}

		if (sendTo != null) {		
			if (user.getRegion().equals("")) {
				user.setRegion(null);
			}

			if (user.getCountry().equals("")) {
				user.setCountry(null);
			}

			if (user.getCompanyType().equals("")) {
				user.setCompanyType(null);
			}

			if (user.getCompanySubType().equals("")) {
				user.setCompanySubType(null);
			}			
			
			List<User> userList = userService.findUsersByExample(user);
			
			userService.excludeUsers(userList, exclusionEmailList);			

			if (userService.emailToUsers(sendFrom, userList, emailSubject, emailTemplate)) {
				return "sendemailpage1";
			} else {
				return ERROR;
			}
		}
		
		return "sendemailpage1";
	}

	public String getSendFrom() {
		return sendFrom;
	}

	public void setSendFrom(String sendFrom) {
		this.sendFrom = sendFrom;
	}

	public String getSendTo() {
		return sendTo;
	}

	public void setSendTo(String sendTo) {
		this.sendTo = sendTo;
	}

	public Visitor getVisitor() {
		return visitor;
	}

	public void setVisitor(Visitor visitor) {
		this.visitor = visitor;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getExclusionEmailList() {
		return exclusionEmailList;
	}

	public void setExclusionEmailList(String exclusionEmailList) {
		this.exclusionEmailList = exclusionEmailList;
	}

	public String getEmailTemplate() {
		return emailTemplate;
	}

	public void setEmailTemplate(String emailTemplate) {
		this.emailTemplate = emailTemplate;
	}

	public String getNext() {
		return next;
	}

	public void setNext(String next) {
		this.next = next;
	}

	public String getEmailSubject() {
		return emailSubject;
	}

	public void setEmailSubject(String emailSubject) {
		this.emailSubject = emailSubject;
	}

}
